package ar.edu.utn.frsf.cidisi.bbp.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.edu.utn.frsf.cidisi.bbp.Node;
import ar.edu.utn.frsf.cidisi.bbp.Tree;

public class GenerateMBDAction implements IObjectActionDelegate {
	
	private Tree bbp; 

	private Shell shell;




	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	final public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
	
	
	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	final public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.size() == 1
					&& structuredSelection.getFirstElement() instanceof Tree) {
				((Tree)structuredSelection.getFirstElement()).eContainer();
				bbp = (Tree)structuredSelection.getFirstElement();
			}
		}
	}
	
	
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	final public void run(IAction action) {
		
//	    Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().


		Bbp2Mdb bbp2mbd = new Bbp2Mdb(bbp);
		

//		// Saves the net to a pnml file
//		bbp2mbd.createBBPFile(newFileName);
		
		Node root = bbp.getRoot();

		bbp2mbd.generateMBD(root);
		
		MessageDialog.openInformation(
				shell,
				"Minimal Behavioral Decomposition",
				"MBD completed.");	
////				"Generate GI-Net was executed.");	
//		
////		AbstractEPNKJob job = createJob(petrinet,defaultInput);
////		
////		if (job.prepare()) {
////			defaultInput = job.getInput();
////			// eki: Since there are porblems with user jobs, I do not
////			// start the job as a user job right now.
////			// job.setUser(true);
////			job.setPriority(Job.SHORT);
////			job.schedule(); 
////		}
	}
	
	
	

	

}
